package com.glowdayz.glowmee.dao;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.DateTime;
import com.glowdayz.common.MultipartBox;
import com.glowdayz.common.MultipartBox.FileInfo;
import com.glowdayz.common.PageHandler;
import com.glowdayz.glowmee.cmd.AdminTableCMD;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

/** 관리자 테이블 기능을 구현하는 클래스 */
public class AdminTableDAO {
	
	static Logger logger = Logger.getLogger(AdminTableDAO.class);
	
	private static AdminTableDAO instance = null;
	
	DBHandler db = null;
	
	public static AdminTableDAO getInstance() {
		if (instance == null) {
			instance = new AdminTableDAO();
		}
		return instance;
	}
	
	public AdminTableDAO() {
		db = DBHandler.getInstance();
	}
	
	/**
	 * 관리자 테이블 활성/비활성처리
	 * @param cmd
	 * @return int
	 */
	public int isDisplayAdminTable(AdminTableCMD cmd) throws Exception {
		logger.debug("[isDisplayAdminTable] ---------- Method Start ----------");
		logger.debug("[isDisplayAdminTable][PARAM] AdminTableCMD cmd=" + cmd.toString());
		
		int result = 0;
		
		String sql = "UPDATE admintable "
				+ "SET isDisplay=?, modified_date=? "
				+ "WHERE idAdminBoard=?";
		PreparedStatement pstmt = db.getPrepareStatement(sql);
		
		try {
			pstmt.setInt(1, cmd.getIsDisplay());
			pstmt.setString(2, DateTime.getInstance().getNowDateTimeString());
			pstmt.setInt(3, cmd.getIdAdminBoard());
			
			DBHandler.getInstance().beginTrans();
			result = pstmt.executeUpdate();
			
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			DBHandler.getInstance().rollback();
			logger.error("[isDisplayAdminTable] " + e.getLocalizedMessage());
			logger.error("[isDisplayAdminTable] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result > 0) {
			DBHandler.getInstance().commit();
		} else {
			DBHandler.getInstance().rollback();
		}
		
		logger.debug("[isDisplayAdminTable][RETURN] result=" + result);
		logger.debug("[isDisplayAdminTable] ---------- Method End ----------");
		
		return result;
		
	}
	
	/**
	 * 공지사항 추가
	 * @param cmd
	 * @return int
	 */
	public int insertNotice(AdminTableCMD cmd) throws Exception {
		logger.debug("[insertNotice] ---------- Method Start ----------");
		logger.debug("[insertNotice][PARAM] AdminTableCMD cmd=" + cmd.toString());
		
		int result = 0;
		
		PreparedStatement pstmt = null;
		
		String sql = "INSERT INTO admintable("
				+ "boardType, boardTitle, boardText, isDisplay, readCount, create_date"
				+ ") VALUES ('NOTICE', ?, ?, 1, 0, ?)";
		
		pstmt = db.getPrepareStatement(sql);
		
		DBHandler.getInstance().beginTrans();
		try {
			pstmt.setString(1, cmd.getBoardTitle());
			pstmt.setString(2, cmd.getBoardText());
			pstmt.setString(3, DateTime.getInstance().getNowDateTimeString());
			result = pstmt.executeUpdate();
			
			if (result > 0) {
				// 자동증가 일련변호 조회
				ResultSet rs = pstmt.getGeneratedKeys();
				if (rs.next()) {
					result = rs.getInt(1);
				}
				rs.close();
				rs = null;
			}
			
			pstmt.close();
			pstmt = null;
			
			if (cmd.getImage() != null) {
				String fileSql = "INSERT INTO attachefile ("
						+ "tableName, tableKey, fileOrgName, fileSaveName, fileDir, fileSize, fileType, isDisplay, create_date, isUser"
						+ ") VALUES ('admintable', ?, ?, ?, ?, ?, ?, 1, ?, 0)";
				
				
				int fileResult = 0;
				
				FileInfo item = cmd.getImage();
				
				pstmt = db.getPrepareStatement(fileSql);
				pstmt.setInt(1, result);
				pstmt.setString(2, item.getFileOrgName());
				pstmt.setString(3, item.getFileSaveName());
				pstmt.setString(4, item.getFileDir());
				pstmt.setLong(5, item.getFileSize());
				pstmt.setString(6, item.getFileType());
				pstmt.setString(7, DateTime.getInstance().getNowDateTimeString());
				
				fileResult = pstmt.executeUpdate();
				
				if (fileResult > 0) {
					// 자동증가 일련변호 조회
					ResultSet rs = pstmt.getGeneratedKeys();
					if (rs.next()) {
						fileResult = rs.getInt(1);
					}
					rs.close();
					rs = null;
				} else {
					// 모든 데이터 저장 취소
					pstmt.close();
					pstmt = null;
					DBHandler.getInstance().rollback();
					return -1;
				}
				
				pstmt.close();
				pstmt = null;
					
				
				String update = "UPDATE admintable "
						+ "SET adminTableImg=? "
						+ "WHERE idAdminBoard=?";
				pstmt = db.getPrepareStatement(update);
				pstmt.setInt(1, fileResult);
				pstmt.setInt(2, result);
				result = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
			}
		} catch (SQLException e) {
			DBHandler.getInstance().rollback();
			logger.error("[insertNotice] " + e.getLocalizedMessage());
			logger.error("[insertNotice] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result > 0) {
			DBHandler.getInstance().commit();
		} else {
			DBHandler.getInstance().rollback();
		}
		
		logger.debug("[insertNotice][RETURN] result=" + result);
		logger.debug("[insertNotice] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 공지사항 수정
	 * @param cmd
	 * @return int
	 */
	public int updateNotice(AdminTableCMD cmd, String[] delete_file) throws Exception {
		logger.debug("[updateNotice] ---------- Method Start ----------");
		logger.debug("[updateNotice][PARAM] AdminTableCMD cmd=" + cmd.toString());
		
		int result = 0;
		
		DBHandler.getInstance().beginTrans();
		PreparedStatement pstmt = null;
		
		// (1) 삭제할 파일에 대한 리스트를 불러온다.
		if (delete_file != null) {
			String in_cause = "(";
			
			for (int i=0; i<delete_file.length; i++) {
				in_cause += "'" + delete_file[i] + "'";
				
				if (i + 1 < delete_file.length) {
					in_cause += ",";
				}
			}
			
			in_cause += ")";
			
			// 삭제 대상을 불러올 SQL
			String fileSql = "SELECT fileOrgName, fileSaveName, fileDir, fileSize, fileType "
					+ "FROM attachefile "
					+ "WHERE tableName='admintable' AND tableKey=? AND fileSaveName in " + in_cause;
			pstmt = DBHandler.getInstance().getPrepareStatement(fileSql);
			
			try {
				pstmt.setInt(1, cmd.getIdAdminBoard());
				ResultSet fileRs = pstmt.executeQuery();
				if (fileRs != null) {
					// 삭제 대상을 만들기 위한 ArrayList 작성
					if (fileRs.next()) {
						String fileOrgName = fileRs.getString(1);
						String fileSaveName = fileRs.getString(2);
						String fileDir = fileRs.getString(3);
						int fileSize = fileRs.getInt(4);
						String fileType = fileRs.getString(5);
						
						FileInfo info = new FileInfo("", fileOrgName, fileSaveName, fileDir, fileSize, fileType);
						// (2) 삭제 대상에 대한 처리
						MultipartBox mbox = MultipartBox.getInstance();
						mbox.deleteFile(info);
					}
					fileRs.close();
					fileRs = null;
				}
				pstmt.close();
				pstmt = null;
			} catch (SQLException e) {
				DBHandler.getInstance().rollback();
				logger.error("[updateNotice] " + e.getLocalizedMessage());
				logger.error("[updateNotice] " + e.getSQLState());
			} finally {
				if (pstmt != null) {
					pstmt.close();
					pstmt = null;
				}
			}
			
			String update = "UPDATE admintable "
					+ "SET adminTableImg=null "
					+ "WHERE idAdminBoard=?";
			pstmt = db.getPrepareStatement(update);
			pstmt.setInt(1, cmd.getIdAdminBoard());
			pstmt.executeUpdate();
			pstmt.close();
			pstmt = null;
				
			// 게시물 데이터 삭제처리
			String delSql = "DELETE FROM attachefile "
					+ "WHERE tableName='admintable' AND tableKey=? AND fileSaveName in " + in_cause;
			
			pstmt = DBHandler.getInstance().getPrepareStatement(delSql);
			try {
				pstmt.setInt(1, cmd.getIdAdminBoard());
				
				pstmt.executeUpdate();
				
				pstmt.close();
				pstmt = null;
			} catch (SQLException e) {
				DBHandler.getInstance().rollback();
				logger.error("[updateNotice] " + e.getLocalizedMessage());
				logger.error("[updateNotice] " + e.getSQLState());
			} finally {
				if (pstmt != null) {
					pstmt.close();
					pstmt = null;
				}
			}
		}
		
		// (3) 게시물 정보 갱신
		String sql = "UPDATE admintable "
				+ "SET boardTitle=?, boardText=?, modified_date=? "
				+ "WHERE idAdminBoard=?";
		pstmt = db.getPrepareStatement(sql);
		
		try {
			pstmt.setString(1, cmd.getBoardTitle());
			pstmt.setString(2, cmd.getBoardText());
			pstmt.setString(3, DateTime.getInstance().getNowDateTimeString());
			pstmt.setInt(4, cmd.getIdAdminBoard());
			
			result = pstmt.executeUpdate();
			
			pstmt.close();
			pstmt = null;
			
			if (cmd.getImage() != null) {
				// (4) 새롭게 업로드 된 파일 정보 처리
				String fileSql = "INSERT INTO attachefile ("
						+ "tableName, tableKey, fileOrgName, fileSaveName, fileDir, fileSize, fileType, isDisplay, create_date, isUser"
						+ ") VALUES ('admintable', ?, ?, ?, ?, ?, ?, 1, ?, 0)";
				
				int fileResult = 0;
				
				FileInfo item = cmd.getImage();
				
				pstmt = db.getPrepareStatement(fileSql);
				pstmt.setInt(1, cmd.getIdAdminBoard());
				pstmt.setString(2, item.getFileOrgName());
				pstmt.setString(3, item.getFileSaveName());
				pstmt.setString(4, item.getFileDir());
				pstmt.setLong(5, item.getFileSize());
				pstmt.setString(6, item.getFileType());
				pstmt.setString(7, DateTime.getInstance().getNowDateTimeString());
				
				fileResult = pstmt.executeUpdate();
				
				if (fileResult > 0) {
					// 자동증가 일련변호 조회
					ResultSet rs = pstmt.getGeneratedKeys();
					if (rs.next()) {
						fileResult = rs.getInt(1);
					}
					rs.close();
					rs = null;
				} else {
					// 모든 데이터 저장 취소
					pstmt.close();
					pstmt = null;
					DBHandler.getInstance().rollback();
					return -1;
				}
				pstmt.close();
				pstmt = null;
				
				String update = "UPDATE admintable "
						+ "SET adminTableImg=? "
						+ "WHERE idAdminBoard=?";
				
				pstmt = db.getPrepareStatement(update);
				
				pstmt.setInt(1, fileResult);
				pstmt.setInt(2, cmd.getIdAdminBoard());
				
				result = pstmt.executeUpdate();
				
				pstmt.close();
				pstmt = null;
			}
		} catch (SQLException e) {
			DBHandler.getInstance().rollback();
			logger.error("[updateNotice] " + e.getLocalizedMessage());
			logger.error("[updateNotice] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result > 0) {
			DBHandler.getInstance().commit();
		} else {
			DBHandler.getInstance().rollback();
		}
		
		logger.debug("[updateNotice][RETURN] result=" + result);
		logger.debug("[updateNotice] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 공지사항 조회
	 * @param idAdminBoard
	 * @return AdminTableCMD
	 * @throws SQLException 
	 */
	public AdminTableCMD getNotice(int idAdminBoard) throws SQLException {
		logger.debug("[getNotice] ---------- Method Start ----------");
		logger.debug("[getNotice][PARAM] idAdminBoard=" + idAdminBoard);
		
		AdminTableCMD result = null;
		String sql = "SELECT idAdminBoard, boardType, boardTitle, boardText, adminTableImg, isDisplay, readCount, create_date "
				+ "FROM admintable "
				+ "WHERE idAdminBoard=?";
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
		
		try {
			pstmt.setInt(1, idAdminBoard);
			ResultSet rs = pstmt.executeQuery();
			
			if (rs != null) {
				if (rs.next()) {
					int num = rs.getInt(1);
					String boardType = rs.getString(2);
					String boardTitle = rs.getString(3);
					String boardText = rs.getString(4);
					int adminTableImg =rs.getInt(5);
					int isDisplay = rs.getInt(6);
					int readCount = rs.getInt(7);
					String create_date = rs.getString(8);
					
					FileInfo image = this.getImage(num);
					
					result = new AdminTableCMD(num, boardType, boardTitle, boardText, adminTableImg, image, isDisplay, readCount, create_date);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error("[getNotice] " + e.getLocalizedMessage());
			logger.error("[getNotice] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result != null) {
			logger.debug("[getNotice][RETURN] result=" + result.toString());
		}
		logger.debug("[getNotice] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 공지사항 리스트
	 * @param page
	 * @param count
	 * @param board
	 * @param order
	 * @return ArrayList<AdminTableCMD>
	 */
	public ArrayList<AdminTableCMD> getNoticeList(int page, int count, String board, String order) throws Exception {
		logger.debug("[getNoticeList] ---------- Method Start ----------");
		logger.debug("[getNoticeList][PARAM] page=" + page);
		logger.debug("[getNoticeList][PARAM] count=" + count);
		logger.debug("[getNoticeList][PARAM] boardType=" + board);
		logger.debug("[getNoticeList][PARAM] order=" + order);
		
		ArrayList<AdminTableCMD> result = null;
		int cnt = db.getCount("admintable", "idAdminBoard", "boardType="  + "'" + board + "'");
		
		String sql = null;
		if (order == null) {
			sql = "SELECT idAdminBoard, boardType, boardTitle, boardText, isDisplay, readCount, create_date "
					+ "FROM admintable WHERE boardType=? "
					+ "ORDER BY create_date DESC "
					+ "LIMIT ?, ?";
		} else {
			sql = "SELECT idAdminBoard, boardType, boardTitle, boardText, isDisplay, readCount, create_date "
					+ "FROM admintable WHERE boardType=? "
					+ "ORDER BY " + order + " "
					+ "LIMIT ?, ?";
		}
		
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
		
		if (cnt == 0) {
			PageHandler.getInstance(page, cnt);
		} else {
			// 페이징 처리 --> 한 페이지에 count개씩, 페이지번호는 10개 출력
			PageHandler.getInstance(page, cnt, count, 10);
		}
		
		try {
			pstmt.setString(1, board);
			pstmt.setInt(2, PageHandler.getInstance().getStartRec());
			pstmt.setInt(3, PageHandler.getInstance().getPageArticleCount());
			ResultSet rs = pstmt.executeQuery();
			
			if (rs != null) {
				
				// SQL문장에 이상이 없을 경우에만 list 객체를 할당한다.
				// 이 객체가 null이라면 SQL실행에 실패한 경우이다.
				// 객체가 생성되지만, add()가 실행되지 않아서, size()값이 0이라면
				// 조건에 맞는 데이터가 없는 경우이다.
				result = new ArrayList<AdminTableCMD>();
				
				while(rs.next()) {
					int idAdminBoard = rs.getInt(1);
					String boardType = rs.getString(2);
					String boardTitle = rs.getString(3);
					String boardText = rs.getString(4);
					int isDisplay = rs.getInt(5);
					int readCount = rs.getInt(6);
					String create_date = rs.getString(7);
					AdminTableCMD item = new AdminTableCMD(idAdminBoard, boardType, boardTitle, boardText, isDisplay, readCount, create_date);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error("[getNoticeList] " + e.getLocalizedMessage());
			logger.error("[getNoticeList] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
			}
		}
		
		if (result != null) {
			logger.debug("[getNoticeList][RETURN] result_count=" + result.size());
		}
		logger.debug("[getNoticeList] ---------- Method End ----------");
		
		return result;
	}
	
	/** 공지사항 xml */
	public ArrayList<AdminTableCMD> getNoticeList(int page, int count, int idAdmin) throws Exception {
		logger.debug("[getNoticeList] ---------- Method Start ----------");
		logger.debug("[getNoticeList][PARAM] page=" + page);
		logger.debug("[getNoticeList][PARAM] count=" + count);
		
		ArrayList<AdminTableCMD> result = null;
		int cnt = db.getCount("admintable", "idAdminBoard", "boardType='NOTICE'");
		if (count == -1) {
			count = cnt;
		}
		
		String sql = null;
		if (idAdmin > 0) {
			sql = "SELECT idAdminBoard, boardTitle, boardText, create_date "
					+ "FROM admintable WHERE boardType='NOTICE' AND isDisplay=1 AND idAdminBoard=" + idAdmin + " "
					+ "ORDER BY create_date DESC "
					+ "LIMIT ?, ?";
		} else {
			sql = "SELECT idAdminBoard, boardTitle, boardText, create_date "
					+ "FROM admintable WHERE boardType='NOTICE' AND isDisplay=1 "
					+ "ORDER BY create_date DESC "
					+ "LIMIT ?, ?";
		}
		
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
		
		if (cnt == 0) {
			PageHandler.getInstance(page, cnt);
		} else {
			// 페이징 처리 --> 한 페이지에 count개씩, 페이지번호는 10개 출력
			PageHandler.getInstance(page, cnt, count, 10);
		}
		
		try {
			ResultSet rs = null;
			
			if (idAdmin > 0) {
				pstmt.setInt(1, 0);
				pstmt.setInt(2, 1);
				rs = pstmt.executeQuery();
			} else {
				pstmt.setInt(1, PageHandler.getInstance().getStartRec());
				pstmt.setInt(2, PageHandler.getInstance().getPageArticleCount());
				rs = pstmt.executeQuery();
			}
			
			if (rs != null) {
				
				// SQL문장에 이상이 없을 경우에만 list 객체를 할당한다.
				// 이 객체가 null이라면 SQL실행에 실패한 경우이다.
				// 객체가 생성되지만, add()가 실행되지 않아서, size()값이 0이라면
				// 조건에 맞는 데이터가 없는 경우이다.
				result = new ArrayList<AdminTableCMD>();
				
				while(rs.next()) {
					int idAdminBoard = rs.getInt(1);
					String boardTitle = rs.getString(2);
					String boardText = rs.getString(3);
					String create_date = rs.getString(4);
					
					FileInfo image = this.getImage(idAdminBoard);
					
					AdminTableCMD item = new AdminTableCMD(idAdminBoard, boardTitle, boardText, image, create_date);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error("[getNoticeList] " + e.getLocalizedMessage());
			logger.error("[getNoticeList] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
			}
		}
		
		if (result != null) {
			logger.debug("[getNoticeList][RETURN] result_count=" + result.size());
		}
		logger.debug("[getNoticeList] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 이벤트 추가
	 * @param cmd
	 * @return int
	 */
	public int insertEvent(AdminTableCMD cmd) throws Exception {
		logger.debug("[insertEvent] ---------- Method Start ----------");
		logger.debug("[insertEvent][PARAM] AdminTableCMD cmd=" + cmd.toString());
		
		int result = 0;
		
		PreparedStatement pstmt = null;
		
		String sql = "INSERT INTO admintable("
				+ "boardType, eventLinkType, eventLinkCode, boardTitle, boardText, idBrand, start_date, end_date, isDisplay, readCount, create_date"
				+ ") VALUES ('EVENT', ?, ?, ?, ?, ?, ?, ?, 1, 0, ?)";
		
		pstmt = db.getPrepareStatement(sql);
		
		DBHandler.getInstance().beginTrans();
		try {
			pstmt.setInt(1, cmd.getEventLinkType());
			pstmt.setString(2, cmd.getEventLinkCode());
			pstmt.setString(3, cmd.getBoardTitle());
			pstmt.setString(4, cmd.getBoardText());
			pstmt.setInt(5, cmd.getIdBrand());
			pstmt.setString(6, cmd.getStart_date());
			pstmt.setString(7, cmd.getEnd_date());
			pstmt.setString(8, DateTime.getInstance().getNowDateTimeString());
			result = pstmt.executeUpdate();
			
			if (result > 0) {
				// 자동증가 일련변호 조회
				ResultSet rs = pstmt.getGeneratedKeys();
				if (rs.next()) {
					result = rs.getInt(1);
				}
				rs.close();
				rs = null;
			}
			
			pstmt.close();
			pstmt = null;
			
			if (cmd.getImage() != null) {
				
				String fileSql = "INSERT INTO attachefile ("
						+ "tableName, tableKey, fileOrgName, fileSaveName, fileDir, fileSize, fileType, isDisplay, create_date, isUser"
						+ ") VALUES ('admintable', ?, ?, ?, ?, ?, ?, 1, ?, 0)";
				
				int fileResult = 0;
				
				FileInfo item = cmd.getImage();
				
				pstmt = db.getPrepareStatement(fileSql);
				pstmt.setInt(1, result);
				pstmt.setString(2, item.getFileOrgName());
				pstmt.setString(3, item.getFileSaveName());
				pstmt.setString(4, item.getFileDir());
				pstmt.setLong(5, item.getFileSize());
				pstmt.setString(6, item.getFileType());
				pstmt.setString(7, DateTime.getInstance().getNowDateTimeString());
				
				fileResult = pstmt.executeUpdate();
				
				if (fileResult > 0) {
					// 자동증가 일련변호 조회
					ResultSet rs = pstmt.getGeneratedKeys();
					if (rs.next()) {
						fileResult = rs.getInt(1);
					}
					rs.close();
					rs = null;
				} else {
					// 모든 데이터 저장 취소
					pstmt.close();
					pstmt = null;
					DBHandler.getInstance().rollback();
					return -1;
				}
				
				pstmt.close();
				pstmt = null;
					
				
				String update = "UPDATE admintable "
						+ "SET adminTableImg=? "
						+ "WHERE idAdminBoard=?";
				pstmt = db.getPrepareStatement(update);
				pstmt.setInt(1, fileResult);
				pstmt.setInt(2, result);
				result = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
			}
		} catch (SQLException e) {
			DBHandler.getInstance().rollback();
			logger.error("[insertEvent] " + e.getLocalizedMessage());
			logger.error("[insertEvent] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result > 0) {
			DBHandler.getInstance().commit();
		} else {
			DBHandler.getInstance().rollback();
		}
		
		logger.debug("[insertEvent][RETURN] result=" + result);
		logger.debug("[insertEvent] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 이벤트 수정
	 * @param cmd
	 * @param delete_file
	 * @return int
	 */
	public int updateEvent(AdminTableCMD cmd) throws SQLException {
		logger.debug("[updateEvent] ---------- Method Start ----------");
		logger.debug("[updateEvent][PARAM] AdminTableCMD cmd=" + cmd.toString());
		
		int result = 0;
		
		DBHandler.getInstance().beginTrans();
		PreparedStatement pstmt = null;
		
		// (3) 게시물 정보 갱신
		String sql = "UPDATE admintable "
				+ "SET boardTitle=?, idBrand=?, start_date=?, end_date=?, boardText=?, eventLinkType=?, eventLinkCode=?, isDisplay=?, modified_date=? "
				+ "WHERE idAdminBoard=?";
		pstmt = db.getPrepareStatement(sql);
		
		try {
			pstmt.setString(1, cmd.getBoardTitle());
			pstmt.setInt(2, cmd.getIdBrand());
			pstmt.setString(3, cmd.getStart_date());
			pstmt.setString(4, cmd.getEnd_date());
			pstmt.setString(5, cmd.getBoardText());
			pstmt.setInt(6, cmd.getEventLinkType());
			pstmt.setString(7, cmd.getEventLinkCode());
			pstmt.setInt(8, cmd.getIsDisplay());
			pstmt.setString(9, DateTime.getInstance().getNowDateTimeString());
			pstmt.setInt(10, cmd.getIdAdminBoard());
			
			result = pstmt.executeUpdate();
			
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			DBHandler.getInstance().rollback();
			logger.error("[updateProduct] " + e.getLocalizedMessage());
			logger.error("[updateProduct] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result > 0) {
			DBHandler.getInstance().commit();
		} else {
			DBHandler.getInstance().rollback();
		}
		
		logger.debug("[updateProduct][RETURN] result=" + result);
		logger.debug("[updateProduct] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 이벤트 조회
	 * @param idAdminBoard
	 * @return AdminTableCMD
	 */
	public AdminTableCMD getEvent(int idAdminBoard) throws SQLException {
		logger.debug("[getEvent] ---------- Method Start ----------");
		logger.debug("[getEvent][PARAM] idAdminBoard=" + idAdminBoard);
		
		AdminTableCMD result = null;
		String sql = "SELECT idAdminBoard id, boardTitle, a.idBrand, b.brandTitle, boardText, a.start_date, a.end_date, a.isDisplay, "
				+ "eventLinkType, eventLinkCode, (SELECT COUNT(*) FROM eventcomment c WHERE c.idAdminBoard=id) count "
				+ "FROM admintable a, brand b "
				+ "WHERE boardType='EVENT' AND a.idBrand=b.idBrand AND idAdminBoard=?";
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
		
		try {
			pstmt.setInt(1, idAdminBoard);
			ResultSet rs = pstmt.executeQuery();
			
			if (rs != null) {
				if (rs.next()) {
					int num = rs.getInt(1);
					String boardTitle = rs.getString(2);
					int idBrand = rs.getInt(3);
					String brandTitle = rs.getString(4);
					String boardText =rs.getString(5);
					String start_date = rs.getString(6);
					String end_date = rs.getString(7);
					int isDisplay = rs.getInt(8);
					int eventLinkType = rs.getInt(9);
					String eventLinkCode = rs.getString(10);
					int comment_count = rs.getInt(11);
					
					FileInfo image = this.getImage(num);
					
					result = new AdminTableCMD(idAdminBoard, eventLinkType, eventLinkCode, boardTitle, boardText, idBrand, brandTitle, 
							start_date, end_date, image, isDisplay, comment_count);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error("[getEvent] " + e.getLocalizedMessage());
			logger.error("[getEvent] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result != null) {
			logger.debug("[getEvent][RETURN] result=" + result.toString());
		}
		logger.debug("[getEvent] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 이벤트 리스트 조회
	 * @param page
	 * @param count
	 * @param order
	 * @param where
	 * @return ArrayList<AdminTableCMD>
	 */
	public ArrayList<AdminTableCMD> getEventList(int page, int count, String order, String where) throws Exception {
		logger.debug("[getEventList] ---------- Method Start ----------");
		logger.debug("[getEventList][PARAM] page=" + page);
		logger.debug("[getEventList][PARAM] count=" + count);
		logger.debug("[getEventList][PARAM] order=" + order);
		logger.debug("[getEventList][PARAM] where=" + where);
		
		ArrayList<AdminTableCMD> result = null;
		int cnt;
		if (where == null) {
			cnt = db.getCount("admintable", "idAdminBoard", "boardType='EVENT'");
		} else {
			cnt = db.getCount("admintable a", "idAdminBoard", "boardType='EVENT'" + where);
		}
		
		String sql = null;
		if (order == null && where == null) {
			sql = "SELECT idAdminBoard id, boardTitle, a.idBrand, b.brandTitle, a.start_date, a.end_date, a.isDisplay, readCount, a.create_date, "
					+ "(SELECT COUNT(*) FROM eventcomment c WHERE c.idAdminBoard=id) count "
					+ "FROM admintable a, brand b WHERE boardType='EVENT' AND a.idBrand=b.idBrand "
					+ "ORDER BY create_date DESC "
					+ "LIMIT ?, ?";
		} else if (order != null && where != null) {
			sql = "SELECT idAdminBoard id, boardTitle, a.idBrand, b.brandTitle, a.start_date, a.end_date, a.isDisplay, readCount, a.create_date, "
					+ "(SELECT COUNT(*) FROM eventcomment c WHERE c.idAdminBoard=id) count "
					+ "FROM admintable a, brand b WHERE boardType='EVENT' AND a.idBrand=b.idBrand " + where + " "
					+ "ORDER BY " + order + " "
					+ "LIMIT ?, ?";
		} else if (order != null) {
			sql = "SELECT idAdminBoard id, boardTitle, a.idBrand, b.brandTitle, a.start_date, a.end_date, a.isDisplay, readCount, a.create_date, "
					+ "(SELECT COUNT(*) FROM eventcomment c WHERE c.idAdminBoard=id) count "
					+ "FROM admintable a, brand b WHERE boardType='EVENT' AND a.idBrand=b.idBrand "
					+ "ORDER BY " + order + " "
					+ "LIMIT ?, ?";
		} else if (where != null) {
			sql = "SELECT idAdminBoard id, boardTitle, a.idBrand, b.brandTitle, a.start_date, a.end_date, a.isDisplay, readCount, a.create_date, "
					+ "(SELECT COUNT(*) FROM eventcomment c WHERE c.idAdminBoard=id) count "
					+ "FROM admintable a, brand b WHERE boardType='EVENT' AND a.idBrand=b.idBrand " + where + " "
					+ "ORDER BY create_date DESC "
					+ "LIMIT ?, ?";
		}
		
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
		
		if (cnt == 0) {
			PageHandler.getInstance(page, cnt);
		} else {
			// 페이징 처리 --> 한 페이지에 count개씩, 페이지번호는 10개 출력
			PageHandler.getInstance(page, cnt, count, 10);
		}
		
		try {
			pstmt.setInt(1, PageHandler.getInstance().getStartRec());
			pstmt.setInt(2, PageHandler.getInstance().getPageArticleCount());
			ResultSet rs = pstmt.executeQuery();
			
			if (rs != null) {
				
				// SQL문장에 이상이 없을 경우에만 list 객체를 할당한다.
				// 이 객체가 null이라면 SQL실행에 실패한 경우이다.
				// 객체가 생성되지만, add()가 실행되지 않아서, size()값이 0이라면
				// 조건에 맞는 데이터가 없는 경우이다.
				result = new ArrayList<AdminTableCMD>();
				
				while(rs.next()) {
					int idAdminBoard = rs.getInt(1);
					String boardTitle = rs.getString(2);
					int idBrand = rs.getInt(3);
					String brandTitle = rs.getString(4);
					String start_date = rs.getString(5);
					String end_date = rs.getString(6);
					int isDisplay = rs.getInt(7);
					int readCount = rs.getInt(8);
					String create_date = rs.getString(9);
					int comment_count = rs.getInt(10);
					AdminTableCMD item = new AdminTableCMD(idAdminBoard, boardTitle, idBrand, brandTitle, start_date, end_date, isDisplay, readCount, comment_count, create_date);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error("[getEventList] " + e.getLocalizedMessage());
			logger.error("[getEventList] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
			}
		}
		
		if (result != null) {
			logger.debug("[getEventList][RETURN] result_count=" + result.size());
		}
		logger.debug("[getEventList] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 기타 관리의 정보수정
	 * @param cmdList
	 * @return int
	 */
	public int updateAdminTable(ArrayList<AdminTableCMD> cmdList) throws Exception {
		logger.debug("[updateAdminTable] ---------- Method Start ----------");
		
		int result = 0;
		
		String sql = null;
		PreparedStatement pstmt = null;
		
		try {
			DBHandler.getInstance().beginTrans();
			for (int i=0; i<cmdList.size(); i++) {
				AdminTableCMD cmd = cmdList.get(i);
				if (cmd.getBoardType().equals("FACEBOOK")) {
					sql = "UPDATE admintable SET boardText = ? WHERE boardType = ?";
					pstmt = db.getPrepareStatement(sql);
					pstmt.setString(1, cmd.getBoardText());
					pstmt.setString(2, cmd.getBoardType());
					result = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
				}
				if (cmd.getBoardType().equals("PARTNER")) {
					sql = "UPDATE admintable SET boardText = ? WHERE boardType = ?";
					pstmt = db.getPrepareStatement(sql);
					pstmt.setString(1, cmd.getBoardText());
					pstmt.setString(2, cmd.getBoardType());
					result = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
				}
				if (cmd.getBoardType().equals("INTROIMAGE")) {
					sql = "UPDATE admintable SET boardText = ? WHERE boardType = ?";
					pstmt = db.getPrepareStatement(sql);
					pstmt.setString(1, cmd.getBoardText());
					pstmt.setString(2, cmd.getBoardType());
					result = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
				}
				if (cmd.getBoardType().equals("IPHONE")) {
					sql = "UPDATE admintable SET boardText = ? WHERE boardType = ?";
					pstmt = db.getPrepareStatement(sql);
					pstmt.setString(1, cmd.getBoardText());
					pstmt.setString(2, cmd.getBoardType());
					result = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
				}
				if (cmd.getBoardType().equals("ANDROID")) {
					sql = "UPDATE admintable SET boardText = ? WHERE boardType = ?";
					pstmt = db.getPrepareStatement(sql);
					pstmt.setString(1, cmd.getBoardText());
					pstmt.setString(2, cmd.getBoardType());
					result = pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
				}
			}
		} catch (SQLException e) {
			DBHandler.getInstance().rollback();
			logger.error("[updateAdminTable] " + e.getLocalizedMessage());
			logger.error("[updateAdminTable] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result > 0) {
			DBHandler.getInstance().commit();
		} else {
			DBHandler.getInstance().rollback();
		}
		
		logger.debug("[updateAdminTable][RETURN] result=" + result);
		logger.debug("[updateAdminTable] ---------- Method End ----------");
		
		return result;
	}
	
	/**
	 * 기타 관리의 리스트
	 * @return ArrayList<AdminTableCMD>
	 */
	public ArrayList<AdminTableCMD> getAdminTableList() throws Exception {
		logger.debug("[getAdminTableList] ---------- Method Start ----------");
		
		ArrayList<AdminTableCMD> result = new ArrayList<AdminTableCMD>();
		
		String sql = "SELECT boardType, boardText "
				+ "FROM admintable "
				+ "WHERE boardType='FACEBOOK'";
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
		
		try {
			ResultSet rs = pstmt.executeQuery();
			
			if (rs != null) {
				while(rs.next()) {
					String boardType = rs.getString(1);
					String boardText = rs.getString(2);
					AdminTableCMD item = new AdminTableCMD(boardType, boardText);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			
			sql = "SELECT boardType, boardText "
					+ "FROM admintable "
					+ "WHERE boardType='PARTNER'";
			pstmt = DBHandler.getInstance().getPrepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs != null) {
				while(rs.next()) {
					String boardType = rs.getString(1);
					String boardText = rs.getString(2);
					AdminTableCMD item = new AdminTableCMD(boardType, boardText);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			
			sql = "SELECT boardType, boardText "
					+ "FROM admintable "
					+ "WHERE boardType='INTROIMAGE'";
			pstmt = DBHandler.getInstance().getPrepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs != null) {
				while(rs.next()) {
					String boardType = rs.getString(1);
					String boardText = rs.getString(2);
					AdminTableCMD item = new AdminTableCMD(boardType, boardText);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			
			sql = "SELECT boardType, boardText "
					+ "FROM admintable "
					+ "WHERE boardType='IPHONE'";
			pstmt = DBHandler.getInstance().getPrepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs != null) {
				while(rs.next()) {
					String boardType = rs.getString(1);
					String boardText = rs.getString(2);
					AdminTableCMD item = new AdminTableCMD(boardType, boardText);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			
			sql = "SELECT boardType, boardText "
					+ "FROM admintable "
					+ "WHERE boardType='ANDROID'";
			pstmt = DBHandler.getInstance().getPrepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs != null) {
				while(rs.next()) {
					String boardType = rs.getString(1);
					String boardText = rs.getString(2);
					AdminTableCMD item = new AdminTableCMD(boardType, boardText);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error("[getFirstCategoryList] " + e.getLocalizedMessage());
			logger.error("[getFirstCategoryList] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
			}
		}
		
		if (result != null) {
			logger.debug("[getFirstCategoryList][RETURN] result_count=" + result.size());
		}
		logger.debug("[getFirstCategoryList] ---------- Method End ----------");
		
		return result;
		
	}
	
	/**
	 * 이벤트 리스트 xml
	 * @param type
	 * @return
	 */
	public ArrayList<AdminTableCMD> getEventList(int page, int count, int type, String where) throws Exception {
		logger.debug("[getEventList] ---------- Method Start ----------");
		logger.debug("[getEventList][PARAM] page=" + page);
		logger.debug("[getEventList][PARAM] count=" + count);
		logger.debug("[getEventList][PARAM] type=" + type);
		
		ArrayList<AdminTableCMD> result = null;
		int cnt;
		String nowTime = DateTime.getInstance().getNowDateTimeString();
		if (type == 1) {
			cnt = db.getCount("admintable a, brand b", "idAdminBoard", "boardType='EVENT' AND a.idBrand=b.idBrand AND a.isDisplay=1 AND (a.start_date<" + nowTime + 
					" AND a.end_date>" + nowTime + ")" + where);
		} else {
			cnt = db.getCount("admintable a, brand b", "idAdminBoard", "boardType='EVENT' AND a.idBrand=b.idBrand AND a.isDisplay=1 AND (a.start_date>" + nowTime + 
					" OR a.end_date<" + nowTime + ")" + where);
		}
		
		String sql = null;
		if (type == 1) {
			sql = "SELECT idAdminBoard, boardTitle, a.idBrand, b.brandTitle, a.start_date, a.end_date "
					+ "FROM admintable a, brand b WHERE boardType='EVENT' AND a.idBrand=b.idBrand AND a.isDisplay=1 "
					+ "AND (a.start_date<" + nowTime + " AND a.end_date>" + nowTime + ")" + where + " "
					+ "GROUP BY a.idAdminBoard "
					+ "ORDER BY a.create_date DESC "
					+ "LIMIT ?, ?";
		} else {
			sql = "SELECT idAdminBoard, boardTitle, a.idBrand, b.brandTitle, a.start_date, a.end_date "
					+ "FROM admintable a, brand b WHERE boardType='EVENT' AND a.idBrand=b.idBrand AND a.isDisplay=1 "
					+ "AND (a.start_date>" + nowTime + " OR a.end_date<" + nowTime + ")" + where + " "
					+ "GROUP BY a.idAdminBoard "
					+ "ORDER BY a.create_date DESC "
					+ "LIMIT ?, ?";
		}
		
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
		
		if (cnt == 0) {
			PageHandler.getInstance(page, cnt);
		} else {
			// 페이징 처리 --> 한 페이지에 count개씩, 페이지번호는 10개 출력
			PageHandler.getInstance(page, cnt, count, 10);
		}
		
		try {
			pstmt.setInt(1, PageHandler.getInstance().getStartRec());
			pstmt.setInt(2, PageHandler.getInstance().getPageArticleCount());
			ResultSet rs = pstmt.executeQuery();
			
			if (rs != null) {
				
				// SQL문장에 이상이 없을 경우에만 list 객체를 할당한다.
				// 이 객체가 null이라면 SQL실행에 실패한 경우이다.
				// 객체가 생성되지만, add()가 실행되지 않아서, size()값이 0이라면
				// 조건에 맞는 데이터가 없는 경우이다.
				result = new ArrayList<AdminTableCMD>();
				
				while(rs.next()) {
					int idAdminBoard = rs.getInt(1);
					String boardTitle = rs.getString(2);
					int idBrand = rs.getInt(3);
					String brandTitle = rs.getString(4);
					String start_date = rs.getString(5);
					String end_date = rs.getString(6);
					
					FileInfo image = this.getImage(idAdminBoard);
					
					AdminTableCMD item = new AdminTableCMD(idAdminBoard, boardTitle, idBrand, brandTitle, start_date, end_date, image);
					result.add(item);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error("[getEventList] " + e.getLocalizedMessage());
			logger.error("[getEventList] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
			}
		}
		
		if (result != null) {
			logger.debug("[getEventList][RETURN] result_count=" + result.size());
		}
		logger.debug("[getEventList] ---------- Method End ----------");
		
		return result;
	}
	
	/** 이벤트 참여 맵핑 */
	public int addEventJoin(int idRegister, int idAdminBoard) throws SQLException {
		logger.debug("[addEventJoin] ---------- Method Start ----------");
		
		int result = 0;
		
		PreparedStatement pstmt = null;
		
		String sql = "INSERT INTO eventjoinmapping("
				+ "idRegister, idAdminBoard, create_date"
				+ ") VALUES (?, ?, ?)";
		
		pstmt = db.getPrepareStatement(sql);
		
		DBHandler.getInstance().beginTrans();
		try {
			pstmt.setInt(1, idRegister);
			pstmt.setInt(2, idAdminBoard);
			pstmt.setString(3, DateTime.getInstance().getNowDateTimeString());
			result = pstmt.executeUpdate();
			
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			DBHandler.getInstance().rollback();
			logger.error("[addEventJoin] " + e.getLocalizedMessage());
			logger.error("[addEventJoin] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		
		if (result > 0) {
			DBHandler.getInstance().commit();
		} else {
			DBHandler.getInstance().rollback();
		}
		
		logger.debug("[addEventJoin][RETURN] result=" + result);
		logger.debug("[addEventJoin] ---------- Method End ----------");
		
		return result;
	}
	
	/** 푸쉬 발송 */
	public int sendPush(String pushText) throws SQLException {
		logger.debug("[sendPush] ---------- Method Start ----------");
		
		PreparedStatement pstmt = null;
		
		ArrayList<String> userList = null;
		
		String sql = "SELECT regId "
				+ "FROM register "
				+ "WHERE isActivity=1";
		
		pstmt = db.getPrepareStatement(sql);
		
		try {
			ResultSet rs = pstmt.executeQuery();
			
			if (rs != null) {
				userList = new ArrayList<String>();
				while(rs.next()) {
					String regId = rs.getString(1);
					if (regId != null && !regId.equals("")) {
						userList.add(regId);
					}
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error("[getRegisterList] " + e.getLocalizedMessage());
			logger.error("[getRegisterList] " + e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
			}
		}
		
		for (int i=0; i<userList.size(); i++) {
			String regId = userList.get(i);
			Sender sender = new Sender("AIzaSyBfwDUfvGLZARl6E8nNSC2LVUWPM4Wnosg");
			Message message = new Message.Builder().addData("msg", pushText).build();

			List<String> list = new ArrayList<String>();
			list.add(regId);

			MulticastResult multiResult;
			try {
				multiResult = sender.send(message, list, 5);
				if (multiResult != null) {
					List<Result> resultList = multiResult.getResults();
					for (Result result : resultList) {
						System.out.println(result.getMessageId());
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		logger.debug("[sendPush] ---------- Method End ----------");
		
		return 1;
	}
	
	/**
	 * 이미지 리스트
	 * @param idx
	 * @return ArrayList<FileInfo>
	 */
	public ArrayList<FileInfo> getImageList(int idx) throws SQLException {
		String fileSql = "SELECT fileOrgName, idAttacheFile, fileSaveName, fileDir, fileSize, fileType, sortKey "
				+ "FROM attachefile "
				+ "WHERE tableName='admintable' AND tableKey=? ORDER BY sortKey";
		
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(fileSql);
		ArrayList<FileInfo> imageList = null;
		
		try {
			pstmt.setInt(1, idx);
			ResultSet rs = pstmt.executeQuery();
			if (rs != null) {
				imageList = new ArrayList<FileInfo>();
				
				while(rs.next()) {
					String fileOrgName = rs.getString(1);
					int idAttacheFile = rs.getInt(2);
					String fileSaveName = rs.getString(3);
					String fileDir = rs.getString(4);
					int fileSize = rs.getInt(5);
					String fileType = rs.getString(6);
					int sortKey = rs.getInt(7);
					
					FileInfo info = new FileInfo("", idAttacheFile, fileOrgName, fileSaveName, fileDir, fileSize, fileType, sortKey);
					imageList.add(info);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error(e.getLocalizedMessage());
			logger.error(e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		return imageList;
	}
	
	public FileInfo getImage(int idx) throws SQLException {
		String fileSql = "SELECT fileOrgName, idAttacheFile, fileSaveName, fileDir, fileSize, fileType, sortKey "
				+ "FROM attachefile "
				+ "WHERE tableName='admintable' AND tableKey=? ORDER BY sortKey LIMIT 0, 1";
		
		PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(fileSql);
		FileInfo info = null;
		
		try {
			pstmt.setInt(1, idx);
			ResultSet rs = pstmt.executeQuery();
			if (rs != null) {
				if(rs.next()) {
					String fileOrgName = rs.getString(1);
					int idAttacheFile = rs.getInt(2);
					String fileSaveName = rs.getString(3);
					String fileDir = rs.getString(4);
					int fileSize = rs.getInt(5);
					String fileType = rs.getString(6);
					int sortKey = rs.getInt(7);
					
					info = new FileInfo("", idAttacheFile, fileOrgName, fileSaveName, fileDir, fileSize, fileType, sortKey);
				}
				rs.close();
				rs = null;
			}
			pstmt.close();
			pstmt = null;
		} catch (SQLException e) {
			logger.error(e.getLocalizedMessage());
			logger.error(e.getSQLState());
		} finally {
			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}
		}
		return info;
	}

}
